﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Maps.MapControl.WPF;

namespace Shrestha.Data.EventModel
{
    public class TriEventCollection
    {
        public class Metadata
        {
            public double MinLatitude;
            public double MaxLatitude;
            public double MinLongitude;
            public double MaxLongitude;
            public DateTime MinEventDate;
            public DateTime MaxEventDate;

            public Metadata(double maxla, double minla, double maxlo, double minlo, DateTime maxd, DateTime mind)
            {
                MinLatitude = minla;
                MaxLatitude = maxla;
                MinLongitude = minlo;
                MaxLongitude = maxlo;
                MinEventDate = mind;
                MaxEventDate = maxd;
            }
        }

        public List<TriEvent> TriEvents;
        public LocationCollection EventLocations;
        public Metadata Meta;

        public TriEventCollection()
        {
            TriEvents = new List<TriEvent>();
            EventLocations = new LocationCollection();
        }

        public void PopulateMetaData()
        {
            if (TriEvents != null && TriEvents.Count > 0)
            {
                Meta = new Metadata(
                    (TriEvents.Aggregate((c, d) => c.EventLatitude > d.EventLatitude ? c : d)).EventLatitude,
                    (TriEvents.Aggregate((c, d) => c.EventLatitude <= d.EventLatitude ? c : d)).EventLatitude,
                    (TriEvents.Aggregate((c, d) => c.EventLongitude > d.EventLongitude ? c : d)).EventLongitude,
                    (TriEvents.Aggregate((c, d) => c.EventLongitude <= d.EventLongitude ? c : d)).EventLongitude,
                    (TriEvents.Aggregate((c, d) => c.EventDate > d.EventDate ? c : d)).EventDate,
                    (TriEvents.Aggregate((c, d) => c.EventDate <= d.EventDate ? c : d)).EventDate
                 );

                EventLocations.Clear();
                foreach (Location e in TriEvents.Select(TriEvent => TriEvent.EventLocation).Distinct().ToList())
                {
                    EventLocations.Add(e);
                }
            }
        }
    }
}
